package org.vincen.helloworld.rmi;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

//远程服务类，远程服务类必须继承UnicastRemoteObject，并实现Remote接口
public class ServerImpl extends UnicastRemoteObject implements Server {
	private static final long serialVersionUID = 1756120675486726108L;

	//由于默认构造器必须声明抛出RemoteException
	//因此此处必须显式定义该构造器
	public ServerImpl() throws RemoteException {
	}

	//实现Remote接口必须实现的方法
	public String helloWorld(String name) throws RemoteException {
		return name + ", 您好!";
	}

	public Person getPerson(String name, int age) throws RemoteException {
		return new Person(name, age);
	}

	//下面是服务类的本地方法，不会“暴露”为远程服务。
	public void info() {
		System.out.println("我是本地方法");
	}

	//下面提供程序入口，将远程类实例绑定为本机的服务。
	public static void main(String[] args) throws Exception {
		Server imp = new ServerImpl();
		LocateRegistry.createRegistry(1099);
		Naming.rebind("rmi://:1099/rmi", imp);
	}
}
